package com.ops.uitl.jdbc;

import java.sql.Connection;

public class TransactionManager {

	public static TransactionManager instance = new TransactionManager();

	public static TransactionManager getInstance() {
		return instance;
	}

	public Object execute(String connName, TransactionCallback callback) throws Exception {
		Connection conn = null;
		Object result = null;
		try {
			conn = ConnectionManager.getInstance().getConnection(connName);
			result = callback.execute(conn);
		} catch (Exception e) {
			if (conn != null) {
				conn.rollback();
				ConnectionManager.getInstance().closeConnection(conn);
			}
			throw e;
		}
		if (conn != null) {
			conn.commit();
			ConnectionManager.getInstance().closeConnection(conn);
		}
		return result;
	}
}
